package com.king.test;

import org.kordamp.ikonli.feather.Feather;
import org.kordamp.ikonli.javafx.FontIcon;

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class IconApp extends Application {

    private Label toastMessage;
    private StackPane rootLayout;

    @Override
    public void start(Stage primaryStage) {
        FlowPane flowPane = new FlowPane();
        flowPane.setPadding(new Insets(10));
        flowPane.setHgap(3);
        flowPane.setVgap(3);
        flowPane.setPrefWrapLength(250);

        Feather[] fs = Feather.values();
        for (Feather f : fs) {
            FontIcon icon = FontIcon.of(f);
            icon.setIconSize(32);
            Label label = new Label(f.name(), icon);
            label.setOnMouseClicked(_ -> {
                copyTextToClipboard(f.name());
                showToastMessage("已复制: " + f.name());
            });
            flowPane.getChildren().add(label);
        }

        ScrollPane scrollPane = new ScrollPane(flowPane);
        scrollPane.setFitToWidth(true);

        rootLayout = new StackPane(scrollPane); // Use StackPane to overlay the toast

        toastMessage = new Label();
        toastMessage.setStyle("-fx-background-color: rgba(0, 0, 0, 0.7); -fx-text-fill: white; -fx-padding: 10px; -fx-font-size: 14px; -fx-background-radius: 5px;");
        toastMessage.setVisible(false); // Initially hide the toast
        StackPane.setAlignment(toastMessage, Pos.TOP_CENTER); // Position at the bottom center
        StackPane.setMargin(toastMessage, new Insets(0, 0, 20, 0)); // Add bottom margin

        rootLayout.getChildren().add(toastMessage); // Add the toast to the StackPane

        Scene scene = new Scene(rootLayout, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Feather Icons");
        primaryStage.show();
    }

    private void copyTextToClipboard(String text) {
        ClipboardContent content = new ClipboardContent();
        content.putString(text);
        Clipboard.getSystemClipboard().setContent(content);
    }

    private void showToastMessage(String message) {
        toastMessage.setText(message);
        toastMessage.setVisible(true);

        FadeTransition fadeOut = new FadeTransition(Duration.seconds(3), toastMessage);
        fadeOut.setFromValue(1.0);
        fadeOut.setToValue(0.0);
        fadeOut.setOnFinished(_ -> toastMessage.setVisible(false));
        fadeOut.play();
    }

	public static void run(String[] args) {
		launch(args);
	}

}
